package com.zcz.umengpush.service;

import com.zcz.umengpush.pojo.PushMsg;
import com.zcz.umengpush.util.AndroidNotification;
import com.zcz.umengpush.util.PushClient;
import com.zcz.umengpush.util.android.*;
import com.zcz.umengpush.util.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Demo{
	Logger logger = LoggerFactory.getLogger(getClass());
//	@Autowired
//	RedisUtil redisUtil;
	//RedisUtil redisUtil = new RedisUtil();

	private String appkey = null;
	private String appMasterSecret = null;
	private String timestamp = null;
	private PushClient client = new PushClient();
	//private PushMsg pushMsg =null;

	public Demo(String key, String secret) {
		try {
			appkey = key;
			appMasterSecret = secret;
			//this.pushMsg=pushMsg;
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
	}
	/**
	 * Android广播发送
	 * @throws Exception
	 */
	public void sendAndroidBroadcast() throws Exception {
		AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
		broadcast.setTicker( "Android broadcast ticker");
		broadcast.setTitle(  "中文的title");
		broadcast.setText(   "Android broadcast text");
		broadcast.goAppAfterOpen();
		broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// 如果是测试设备，TODO将production_mode设置为false。
		// 有关如何注册测试设备，请参阅开发人员文档。
		broadcast.setProductionMode();
		// Set customized fields自定义字段
		//broadcast.setExtraField("test", "helloworld");
		client.send(broadcast);
	}
	/**
	 * Android发送单播  列播
	 * @throws Exception
	 */
	public void sendAndroidUnicast(PushMsg pushMsg,Integer userType) throws Exception {
		AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret,userType);
		// 设置您的设备 token
        //取出tokens 删除第一个，号
        System.err.println(pushMsg.getTokens().substring(1)+"取出tokens 删除第一个，号");
		unicast.setDeviceToken( pushMsg.getTokens().substring(1));
		unicast.setTicker(pushMsg.getTitle());
		unicast.setTitle(pushMsg.getTitle());
		unicast.setText(pushMsg.getContent());
		//是否开启声音
		unicast.setPlaySound(true);
		//是否开启震动
		unicast.setPlayVibrate(true);
		//是否开启闪灯
		unicast.setPlayLights(true);
		//自定义字段用于跳转页面   把  推送消息的类型 和 所推送主键
        unicast.goCustomAfterOpen(""+pushMsg.getObjectType()+"|"+pushMsg.getObjectId()+"");
		//unicast.goAppAfterOpen();

		unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		//   如果是测试设备，TODO将production_mode设置为false。
		//   有关如何注册测试设备，请参阅开发人员文档。
		unicast.setProductionMode();
		// Set customized fields  设置自定义字段
		//unicast.setExtraField("test", "helloworld");
		client.send(unicast,pushMsg.getIds());
	}

	/**
	 * Android分组发送
	 * @throws Exception
	 */
	public void sendAndroidGroupcast() throws Exception {
		AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  Construct the filter condition:
		 *  "where":
		 *	{
		 *		"and":
		 *		[
		 *			{"tag":"test"},
		 *			{"tag":"Test"}
		 *		]
		 *	}
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		JSONObject TestTag = new JSONObject();
		testTag.put("tag", "AlOkYphFeNqeTO31VldQPUZ0yEqqAeKBV6AF5QpiZ9vb");
		TestTag.put("tag", "Test");
		tagArray.put(testTag);
		tagArray.put(TestTag);

		System.err.println(tagArray.toString());

		whereJson.put("and", tagArray);

		System.err.println(whereJson.toString());

		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		groupcast.setFilter(filterJson);
		groupcast.setTicker( "Android groupcast ticker");
		groupcast.setTitle(  "中文的title");
		groupcast.setText(   "Android groupcast text");
		groupcast.goAppAfterOpen();
		groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// 如果是测试设备，TODO将production_mode设置为false。
		// 有关如何注册测试设备，请参阅开发人员文档。
		groupcast.setProductionMode();
		client.send(groupcast);
	}
	/**
	 * 发送Android定制cast  开发者通过自有的alias进行推送，可以针对单个或者一批alias进行推送，也可以将alias存放到文件进行发送
	 * @throws Exception
	 */
	public void sendAndroidCustomizedcast() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// 在这里设置别名，如果有多个别名，则使用逗号分隔它们。
		// 如果你有很多别名，你也可以上传一个包含这些别名的文件
		// 使用文件id发送自定义通知。
		String ali = "b6bfe1b7-8e4e-4625-aee3-15091de53fb4,83cfbcd0-6965-450a-87f3-692aa7e06854";
		//哪个app调用 就用哪个app的名称
		String JoyssomPush = "JoyssomPush";
		customizedcast.setAlias(ali, JoyssomPush);
		//customizedcast.setAlias("alias", "alias_type");
		//customizedcast.setFileId("fileId", "aliasType");
		customizedcast.setTicker( "Android customizedcast ticker");
		customizedcast.setTitle(  "中文的title");
		customizedcast.setText(   "Android customizedcast text");
		customizedcast.goAppAfterOpen();
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// 如果是测试设备，将production_mode设置为false。
		// 有关如何注册测试设备，请参阅开发人员文档。
		customizedcast.setProductionMode();
		client.send(customizedcast);
	}
	/**
	 * 发送Android定制的cast文件
	 * @throws Exception
	 */
	public void sendAndroidCustomizedcastFile() throws Exception {
		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
		// TODO在这里设置别名，如果有多个别名，则使用逗号分隔它们。
		// 如果你有很多别名，你也可以上传一个包含这些别名的文件
		//使用文件id发送自定义通知。
		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb"+"\n"+"alias");
		customizedcast.setFileId(fileId, "alias_type");
		customizedcast.setTicker( "Android customizedcast ticker");
		customizedcast.setTitle(  "中文的title");
		customizedcast.setText(   "Android customizedcast text");
		customizedcast.goAppAfterOpen();
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		// 如果是测试设备，TODO将production_mode设置为false。
		// 有关如何注册测试设备，请参阅开发人员文档。
		customizedcast.setProductionMode();
		client.send(customizedcast);
	}
	/**
	 * 发送Android文件转换
	 * @throws Exception
	 */
	public void sendAndroidFilecast() throws Exception {
		AndroidFilecast filecast = new AndroidFilecast(appkey,appMasterSecret);
		// ODO上传您的设备令牌，如果有多个令牌，则使用'\n'来分割它们
		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
		filecast.setFileId( fileId);
		filecast.setTicker( "Android filecast ticker");
		filecast.setTitle(  "中文的title");
		filecast.setText(   "Android filecast text");
		filecast.goAppAfterOpen();
		filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		client.send(filecast);
	}
	/**
	 * 发送IOS广播
	 * @throws Exception
	 */
	public void sendIOSBroadcast() throws Exception {
		//Jedis jedis = redisUtil.getJedis();
		IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
		broadcast.setAlert("IOS 广播测试");
		//查询消息推送用户注册的数据表  并且手机类型是iOS的
		//从用户列表里取出用户的id  并设置角标的数量 自增1
		//Long badge = jedis.incrBy("从数据库里查出来的userid(umenguserid)", 1);
		//添加角标数量
		//broadcast.setBadge(badge.intValue());
		broadcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode  如果您的应用程序处于生产模式，则将“production_mode”设置为“true”
		broadcast.setTestMode();
		//broadcast.setProductionMode();
		// Set customized fields  设置自定义字段
		broadcast.setCustomizedField("test", "helloworld");

		client.send(broadcast);
	}
	/**
	 * IOS发送 单播 或 列播
	 * @throws Exception
	 */
	public void sendIOSUnicast(PushMsg pushMsg) throws Exception {
		IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
		// TODO Set your device token
        //取出tokens 后 删除第一个，号
		unicast.setDeviceToken(pushMsg.getTokens().substring(1));
		//设置 主标题 内容
		//String str = "{'title':"+"'"+pushMsg.getTitle()+"'"+",'body':"+"'"+pushMsg.getContent()+"'"+"}";
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("title", pushMsg.getTitle());
		jsonObject.put("body", pushMsg.getContent());
		unicast.setAlert(jsonObject);
		//添加角标数量
		unicast.setBadge(1);
		unicast.setSound( "true");
		// TODO set 'production_mode' to 'true' if your app is under production mode 测试模式
		//unicast.setTestMode();
		//正式模式
		unicast.setProductionMode();
		// Set customized fields自定义字段  把  推送消息的类型 和 所推送主键
		unicast.setCustomizedField("payload", ""+pushMsg.getObjectType()+'|'+pushMsg.getObjectId()+"");
		client.send(unicast,pushMsg.getIds());
	}
	/**
	 * IOS 组发
	 * @throws Exception
	 */
	public void sendIOSGroupcast() throws Exception {
		IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
		/*  TODO
		 *  Construct the filter condition:
		 *  "where":
		 *	{
		 *		"and":
		 *		[
		 *			{"tag":"iostest"}
		 *		]
		 *	}
		 */
		JSONObject filterJson = new JSONObject();
		JSONObject whereJson = new JSONObject();
		JSONArray tagArray = new JSONArray();
		JSONObject testTag = new JSONObject();
		testTag.put("tag", "iostest");
		tagArray.put(testTag);
		whereJson.put("and", tagArray);
		filterJson.put("where", whereJson);
		System.out.println(filterJson.toString());

		// Set filter condition into rootJson
		groupcast.setFilter(filterJson);
		groupcast.setAlert("IOS 组播测试");
		groupcast.setBadge( 0);
		groupcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		groupcast.setTestMode();
		client.send(groupcast);
	}
	/**
	 * IOS定制发送   开发者通过自有的alias进行推送，可以针对单个或者一批alias进行推送，也可以将alias存放到文件进行发送
	 * @throws Exception
	 */
	public void sendIOSCustomizedcast() throws Exception {
		IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
		// TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
		// And if you have many alias, you can also upload a file containing these alias, then
		// use file_id to send customized notification.
		customizedcast.setAlias("alias", "alias_type");
		customizedcast.setAlert("IOS 个性化测试");
		customizedcast.setBadge( 0);
		customizedcast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		customizedcast.setTestMode();
		client.send(customizedcast);
	}
	/**
	 * 发送IOS文件转换
	 * @throws Exception
	 */
	public void sendIOSFilecast() throws Exception {
		IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
		// TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
		String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
		filecast.setFileId( fileId);
		filecast.setAlert("IOS 文件播测试");
		filecast.setBadge( 0);
		filecast.setSound( "default");
		// TODO set 'production_mode' to 'true' if your app is under production mode
		filecast.setTestMode();
		client.send(filecast);
	}

	public static void main(String[] args) {
		//ios  5d883004570df3e295000f4e
		//		yypgodugn7okezhcrpif1iumo14nn9qc
		//Android  5d8441d44ca35766bd000d6a
		//         h0p3few1yrmbuhqlizsp4rf4us6bmcmn
		//ios
		String appkey = "5d883004570df3e295000f4e";
		String appMasterSecret = "yypgodugn7okezhcrpif1iumo14nn9qc";
		//Android
		//String appkey = "5d8441d44ca35766bd000d6a";
		//String appMasterSecret = "h0p3few1yrmbuhqlizsp4rf4us6bmcmn";
		Demo demo = new Demo(appkey, appMasterSecret);
		//Jedis jedis = redisUtil.getJedis();

		try {
			//广播
			//demo.sendAndroidBroadcast();
			//demo.sendAndroidUnicast();
			//demo.sendIOSBroadcast();
			//demo.sendAndroidUnicast();
			//ios单播
			//demo.sendIOSUnicast();
			//ios广播
			demo.sendIOSBroadcast();
			//	demo.sendAndroidCustomizedcast();
			//demo.sendAndroidGroupcast();
//			demo.sendIOSUnicast();
			/* TODO these methods are all available, just fill in some fields and do the test
			 * demo.sendAndroidCustomizedcastFile();
			 * demo.sendAndroidBroadcast();
			 * demo.sendAndroidGroupcast();
			 * demo.sendAndroidCustomizedcast();
			 * demo.sendAndroidFilecast();
			 *
			 * demo.sendIOSBroadcast();
			 *demo.sendIOSUnicast();
			 * demo.sendIOSGroupcast();
			 * demo.sendIOSCustomizedcast();
			 * demo.sendIOSFilecast();
			 */
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}


}
